const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bindStatus:0,
        cloud:[name=>'',phone=>''],
        name:'',
        phone:'',
        cityIndex:0,
        city:['请选择区域','临沂市','济宁市','菏泽市','枣庄市','其他']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var open=wx.getStorageSync('open'); 
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })            
        that.setData({
            platform:app.globalData.platform,    
            open:open,
             width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl
          })
    },
    goBack:function(){
        wx.navigateBack();
      },
    inputPhone:function(e){
        //console.log(e);
        var phone=e.detail.value;
        this.setData({
        phone:phone
        })
     
      },
      inputName:function(e){
        //console.log(e);
        var name=e.detail.value;
        this.setData({
        name:name
        })
     
      },
    
    bindCloud:function(e){
        let that=this;
        var data=this.data;
        var open=wx.getStorageSync('open');
        var cityIndex=data.cityIndex;
        var city=data.city;
        var citys=city[cityIndex];
        if(cityIndex==0){
            wx.showToast({
              title: '请选择区域',
            })
                return ;
        }
        if(data.name==''){
            wx.showToast({
              title: '请输入姓名',
            })
                return ;
        }
        if(data.phone==''){
            wx.showToast({
              title: '请输入电话',
            })
                return ;
        }
        console.log(data.phone);
        console.log(data.name);
        wx.request({        
        url: app.globalData.platUrl+'home/bindCloud',
        data:{from:1,user_id:open.user_id,username:data.name,userphone:data.phone,city:citys},
        success:res=>{
            console.log(res);
            if(res.statusCode==200){
                var data=res.data;
                wx.showModal({
                  title: '系统提示',
                  content:data.msg,
                  complete: (res) => {
                    if (res.cancel) {
                      
                    }
                
                    if (res.confirm) {
                      
                    }
                  }
                })
            }else{
               wx.showLoading({
                 title: '系统错误',
               }) 
            }
        }
        })
    
    },
    cityChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var cityIndex=this.data.cityIndex;
        var city=this.data.city;
        if(index>0){
            if(index!=cityIndex){
               // this.getArea()
               this.setData({
                cityIndex:index,
               })
            }
        }else{
            wx.showToast({
              title: '选择城市',
            })
        }
     
    },
    getCloud:function(user_id){
        let that=this;
        var city=this.data.city;
        var cityIndex=0;
        wx.request({        
            url: app.globalData.platUrl+'home/getCloud',
            data:{from:1,user_id:user_id},
            success:res=>{
                console.log(res);
                if(res.statusCode==200){
                    var data=res.data;
                    
                    if(data.status==200){
                        city.forEach(function(ele,index) {
                            if(ele==data.city){
                                cityIndex=index;
                            }
                        });
                        that.setData({
                            bindStatus:1,
                            name:data.name,
                            phone:data.phone,
                            cityIndex:cityIndex
                           // cloud:res.cloud  
                        })
                    }
                }else{
                   wx.showLoading({
                     title: '系统错误',
                   }) 
                }
            }
            })
       
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var open=wx.getStorageSync('open');
        if(open.role==0){
            this.getCloud(open.user_id);
        }
               
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})